﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace myBoard.Framework.Controls
{
    /// <summary>
    /// Interaction logic for MarkeeControl.xaml
    /// </summary>
    [ContentProperty("ScrollingContent")]
    public partial class MarkeeControl : UserControl
    {
        private Storyboard _startScrollingSB;
        private double _scrollingSpeed = 1000 / 30; // 30pixels per second
        private bool _isScrolling;
        

        public UIElement ScrollingContent
        {
            get
            {
                return _scrollingContent.Child;
            }
            set
            {
                _scrollingContent.Child = value;
                ((FrameworkElement)_scrollingContent.Child).SizeChanged += new SizeChangedEventHandler(MarkeeControl_SizeChanged);
            }
        }

        void MarkeeControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (((FrameworkElement)this._scrollingContent.Child).ActualWidth > 0 && !_isScrolling)
            {
                Reset();
                AdjustStoryboards();
                _startScrollingSB.Seek(new TimeSpan(0, 0, 0));
                _startScrollingSB.Begin();
                _isScrolling = true;
                _startScrollingSB.Begin();
            }
        }        

        public MarkeeControl()
        {
            InitializeComponent();
            _startScrollingSB = this.Resources["StartScrolling"] as Storyboard;
            _startScrollingSB.Completed += new EventHandler(StartScrollingSB_Completed);
        }

        void StartScrollingSB_Completed(object sender, EventArgs e)
        {
            _startScrollingSB.Stop();
            Reset();
            AdjustStoryboards();
            _startScrollingSB.Seek(new TimeSpan(0, 0, 0));
        }

        private void AdjustStoryboards()
        {
            double scrollingContentWidth = ((FrameworkElement)this._scrollingContent.Child).ActualWidth;
            
            foreach (Timeline timeline in _startScrollingSB.Children)
            {
                switch (timeline.Name)
                {
                    case "MarginAnimation":
                        {
                            ThicknessAnimationUsingKeyFrames marginAnimation = timeline as ThicknessAnimationUsingKeyFrames;
                            marginAnimation.KeyFrames[0].Value = new Thickness(-scrollingContentWidth, 0, 0, 0);
                            marginAnimation.KeyFrames[0].KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, Convert.ToInt32(scrollingContentWidth * _scrollingSpeed)));
                        }
                        break;
                }
            }
        }

        private void Reset()
        {
            _scrollingContent.Margin = new Thickness(this.ActualWidth, 0, 0, 0);
            //_startScrollingSB.Remove(this);
        }        
    }
}